<?php

use swap\check_failed;
use swap\lazy_checker;
use swap\visitor;

class training_controller extends swap\controller {

    public static $layout = 'my';
    
    public static function before_filters() {
        $id = g_int('id', 0);
        
        return array(
            'resume_filter' => array(
                '*' => array('id' => $id),
            ),
        );
    }
    
    public static function before_run() {
        self::role('user', 'redirect_to', 'my-site/login');
    }

    public static function index_action() {
        $id = visitor::get_value('id');
        self::set('id', $id);
        
        // 已有的培训经历
        $list = training_model::get(array('resume_id' => $id), array(array('id' => 'desc'), 0, resume_ext_service::$limits['training']));
        self::set('list', $list);

        self::show_page();
    }

    public static function new_action() {
        // none
    }

    public static function do_new_action() {
        // 拦截
        self::method('post');
        self::csrf('user');

        try {
            $id = g_int('id');
            
            // 校验
            $props = self::check_form(p(), 'new');

            // 检查
            resume_ext_service::can_set_resume_ext($id, 'training');
            $training_id = training_model::add($props);
            resume_ext_service::set_resume_ext($id, 'training', $training_id);
            
            // 成功
            self::json_result(true, '添加成功');
        } catch (resume_ext_except $se) {
            // service except
            self::json_result(false, $se->get_msg(), 0, $se->get_prop_name());
        } catch (check_failed $e) {
            // 失败
            $msg = $e->get_reasons();
            self::json_result(false, current($msg), 0, key($msg));
        }
    }

    public static function edit_action() {
        // resume_id
        self::helper_set('id', g_int('id'));

        // training_id 
        $training_id = g_int('training_id');
        self::helper_set('training_id', $training_id);

        self::show_page();
    }

    public static function do_edit_action() {
        // 拦截
        self::csrf('user');
        self::method('post');

        try {
            // 校验training_id
            $training = self::check_training_id();
            
            // 校验
            $props = self::check_form(p(), 'edit');
            
            // 更新
            training_model::set_by_id($props, $training->id);
            
            // 成功
            self::json_result(true, '编辑成功');
        } catch (check_failed $e) {
            // 失败
            $msg = $e->get_reasons();
            self::json_result(false, current($msg), 0, key($msg));
        }
    }

    public static function del_action() {
        // none
    }

    public static function do_del_action() {
        // 拦截
        self::csrf('user');
        
        // 校验training_id
        $training = self::check_training_id();
        $resume_id = g_int('id');
        
        // 删除
        resume_ext_service::del_resume_ext($resume_id, 'training', $training->id);
        training_model::del_by_id($training->id);
        
        // 成功
        self::json_result(true);
    }

    protected static function check_form($post, $operation) {
        $checker = new lazy_checker($post);
        $checker->check_model_rules('training', $operation);
        extract($checker->get_all());
        // 执行
        $id = g_int('id');
        $props = array(
            'resume_id' => $id,
            'start_time' => strtotime($start_time),
            'end_time' => strtotime($end_time),
            'name' => $name,
            'address' => $address,
            'course' => $course,
            'certificate' => $certificate,
            'introduction' => $introduction,
        );
        return $props;
    }
    
    protected static function check_training_id() {
        $training_id = g_int('training_id', 0);
        $training = training_model::get_by_id($training_id);
        if ($training === null) {
            self::forward_404('非法请求');
        }
        return $training;
    }
    

}